package com.dk.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dk.mapper.WmItemRecptLineMapper;
import com.dk.pojo.*;
import com.dk.result.Result;
import com.dk.result.ResultConstant;
import com.dk.service.RecptLineService;
import com.dk.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author LTZ
 * @create 2023/5/11
 */
@Service
public class RecptLineServiceImpl implements RecptLineService {
    @Autowired
    private WmItemRecptLineMapper recptLineMapper;
    @Autowired
    private JwtUtil jwtUtil;

    @Override
    public List<WmItemRecptLine> getRecptLineInfo(Long recptId) {
        WmItemRecptLineExample example = new WmItemRecptLineExample();
        example.createCriteria().andRecptIdEqualTo(recptId);
        return recptLineMapper.selectByExample(example);
    }

    @Override
    public Result addRecptLine(WmItemRecptLine recptLine, MdItem item, Warehouse warehouse, String token) {
        try {
            Claims claims = jwtUtil.parseJwt(token);
            String subject = claims.getSubject();
            User user = JSONObject.parseObject(subject, User.class);
            recptLine.setCreateBy(user.getUsername());
            recptLine.setCreateTime(new Date());
            recptLine.setItemId(item.getItemId());
            recptLine.setItemName(item.getItemName());
            recptLine.setSpecification(item.getSpecification());
            recptLine.setUnitOfMeasure(item.getUnitOfMeasure());
            recptLine.setWarehouseId(warehouse.getWid().longValue());
            recptLine.setWarehouseCode(warehouse.getCode());
            recptLineMapper.insertSelective(recptLine);
            return  new Result(ResultConstant.CODE,ResultConstant.MESSAGE);
        } catch (Exception e) {
            return new Result(ResultConstant.TOKEN_CODE, ResultConstant.TOKEN_MESSAGE);
        }
    }

    @Override
    public void deleteRecptLine(Long lineId) {
        recptLineMapper.deleteByPrimaryKey(lineId);
    }
}